import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'index.dart';

/// 协议页面视图（通用H5展示）
/// 支持用户协议和隐私政策
class AgreementPage extends GetView<AgreementController> {
  const AgreementPage({super.key});

  Widget _buildView() {
    // 初始化WebViewController
    final webViewController = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setNavigationDelegate(
        NavigationDelegate(
          onPageStarted: (String url) {
            controller.onPageStarted(url);
          },
          onPageFinished: (String url) {
            controller.onPageFinished(url);
          },
          onProgress: (int progress) {
            controller.onProgress(progress);
          },
          onWebResourceError: (WebResourceError error) {
            controller.onWebResourceError(error.description);
          },
        ),
      )
      ..loadRequest(Uri.parse(controller.agreementUrl));

    return Stack(
      children: [
        // WebView
        WebViewWidget(controller: webViewController),
        // 加载进度条
        Obx(() => controller.isLoading.value
            ? Positioned(
                top: 0,
                left: 0,
                right: 0,
                child: LinearProgressIndicator(
                  value: controller.loadProgress.value,
                  backgroundColor: Colors.grey[200],
                  valueColor: const AlwaysStoppedAnimation<Color>(Color(0xFFE94F4F)),
                  minHeight: 2,
                ),
              )
            : const SizedBox()),
        // 错误提示
        Obx(() => controller.hasError.value
            ? Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(
                      Icons.error_outline,
                      size: 64.r,
                      color: Colors.grey[400],
                    ),
                    SizedBox(height: 16.h),
                    Text(
                      '页面加载失败',
                      style: TextStyle(
                        fontSize: 15.sp,
                        color: const Color(0xFF666666),
                      ),
                    ),
                    SizedBox(height: 8.h),
                    Text(
                      controller.errorMessage.value,
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: const Color(0xFF999999),
                      ),
                      textAlign: TextAlign.center,
                    ),
                    SizedBox(height: 20.h),
                    ElevatedButton(
                      onPressed: () {
                        controller.hasError.value = false;
                        controller.isLoading.value = true;
                        // 重新加载
                      },
                      style: ElevatedButton.styleFrom(
                        backgroundColor: const Color(0xFFE94F4F),
                        padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 10.h),
                      ),
                      child: Text(
                        '重新加载',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ],
                ),
              )
            : const SizedBox()),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AgreementController>(
      id: "agreement",
      builder: (_) {
        return Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
            title: Text(
              controller.pageTitle,
              style: TextStyle(
                fontSize: 17.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
            leading: IconButton(
              icon: const Icon(Icons.arrow_back_ios),
              onPressed: () => Get.back(),
            ),
            backgroundColor: Colors.white,
            elevation: 1,
          ),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}

